package com.blb.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community.Annotation.Log;
import com.blb.community.entity.ZyComplaintSuggest;
import com.blb.community.entity.vo.SuggestVO;
import com.blb.community.service.IZyComplaintSuggestService;
import com.blb.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 投诉建议  前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "投诉建议")
@RestController
@RequestMapping("/zy-complaint-suggest")
public class ZyComplaintSuggestController {

    @Autowired
    private IZyComplaintSuggestService zyComplaintSuggestService;

    @ApiOperation("建议列表")
    @PostMapping("/suggests/page")
    public ResponseResult<IPage<SuggestVO>> suggestList(@RequestBody SuggestVO suggestVO) {
        Page<SuggestVO> page = new Page<>(suggestVO.getCurrent(), 5);
        page.setOptimizeJoinOfCountSql(false);
        IPage<SuggestVO> pageList = zyComplaintSuggestService.getPageList(suggestVO, page);
        for (int i = 0; i < pageList.getRecords().size(); i++) {
            SuggestVO suggestVO1 = pageList.getRecords().get(i);
            String image = suggestVO1.getImage();
            String[] split = image.split(",");
            suggestVO1.setImgList(Arrays.asList(split));
        }
        return ResponseResult.ok(pageList);
    }
    @Log(module = "修改投诉建议", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改投诉建议")
    @PutMapping("suggest")
    public ResponseResult<String> updateSuggest(@RequestBody ZyComplaintSuggest zyComplaintSuggest) {
        zyComplaintSuggestService.updateById(zyComplaintSuggest);
        return ResponseResult.ok("ok");
    }
    @Log(module = "删除投诉建议", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除投诉建议")
    @DeleteMapping("suggest/{complaintSuggestId}")
    public ResponseResult<String> deleteById(@PathVariable long complaintSuggestId) {
        zyComplaintSuggestService.removeById(complaintSuggestId);
        return ResponseResult.ok("ok");
    }



    @Log(module = "添加投诉建议", operationType = 1, requestMethod = "PUT")
    @ApiOperation("添加投诉建议")
    @PostMapping("suggest")
    public ResponseResult<String> addSuggest(@RequestBody SuggestVO suggestVO){
        zyComplaintSuggestService.save(suggestVO);
        return ResponseResult.ok("ok");
    };
}
